"use strict";
cc._RF.push(module, 'e8a45yEAtRNqJ9IqqxjS/rW', 'cardManager');
// scripts/cardManager.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.handCardManager = exports.cardsShower = void 0;
var card_1 = require("./card");
var _ = require("lodash");
var resource_1 = require("./resource");
var log = console.log;
var cardsShower = /** @class */ (function () {
    function cardsShower(position, scale) {
        if (scale === void 0) { scale = 0.5; }
        this.cards = [];
        this.scale = 0.5;
        this.anchor = new cc.Node();
        cc.find("Canvas").addChild(this.anchor);
        this.anchor.setPosition(position);
        this.scale = scale;
    }
    cardsShower.prototype.newCard = function (cardinfo, idx) {
        var card = cc.instantiate(cc.find("Canvas").getComponent(resource_1.default).card).getComponent(card_1.default);
        card.InitCard(cardinfo);
        card.node.setScale(this.scale);
        this.anchor.addChild(card.node);
        return card;
    };
    cardsShower.prototype.centifyCard = function () {
        if (!this.cards.length)
            return;
        var cardViewWidth = this.cards[0].node.width / 2 * this.cards[0].node.scaleX;
        var cardBias = -this.cards.length / 2 * cardViewWidth;
        this.cards.map(function (card) {
            card.node.setPosition(cc.v2(cardBias = cardBias + cardViewWidth, 0));
        });
    };
    cardsShower.prototype.display = function (cardsinfo) {
        var _this = this;
        this.cardsinfo = cardsinfo;
        this.cards = [];
        this.anchor.removeAllChildren();
        cardsinfo.map(function (info, idx) {
            _this.cards.push(_this.newCard(info, idx));
        });
        this.centifyCard();
    };
    cardsShower.prototype.destroy = function () {
        this.anchor.destroy();
        this.cards.map(function (card) { return card.destroy(); });
    };
    return cardsShower;
}());
exports.cardsShower = cardsShower;
var handCardManager = /** @class */ (function (_super) {
    __extends(handCardManager, _super);
    function handCardManager() {
        var _this = _super.call(this, cc.v2(0, 70 - cc.view.getVisibleSize().height / 2), 1) || this;
        _this.selectStartIdx = null;
        _this.chooseIdxs = [];
        _this.selectCnt = {
            Value: null, onChange: null,
            set value(val) {
                if (this.onChange && val != this.Value)
                    this.onChange(val, this.Value);
                this.Value = val;
            },
            get value() { return this.Value; }
        };
        _this.selectCnt.onChange = function (value, origin) {
            var inc = value - origin > 0 == value > 0 && value != 0;
            if (value == null || origin == null)
                return; //选择完毕
            _this.operateCards(inc, -1, inc, origin, value); //选择减少时 少减一张
        };
        return _this;
    }
    handCardManager.prototype.newCard = function (cardinfo, idx) {
        var _this = this;
        var halfScreenWidth = cc.view.getVisibleSize().width / 2;
        var card = _super.prototype.newCard.call(this, cardinfo, 1);
        card.Idx = idx;
        card.info = cardinfo;
        card.node.on(cc.Node.EventType.TOUCH_START, function () {
            card.hover(true);
            _this.selectStartIdx = card.Idx;
        });
        card.node.on(cc.Node.EventType.TOUCH_END, function () {
            card.hover(false);
            card.choose();
            _this.selectCnt.value = null;
            _this.updateChooseIdxs();
        });
        card.node.on(cc.Node.EventType.TOUCH_MOVE, function (e) {
            _this.selectCnt.value = Math.floor((e.getLocation().x - card.node.x - halfScreenWidth) / (card.node.width / 2)) + 1;
        });
        card.node.on(cc.Node.EventType.TOUCH_CANCEL, function () {
            if (_this.selectCnt.value == 0)
                return;
            _this.operateCards(false, null);
            _this.selectCnt.value = null;
            _this.updateChooseIdxs();
        });
        return card;
    };
    handCardManager.prototype.updateChooseIdxs = function () {
        this.chooseIdxs = [];
        for (var i = 0; i < this.cards.length; i++)
            if (this.cards[i].getChoose())
                this.chooseIdxs.push(i);
        this.chooseIdxs.sort(function (a, b) { return b - a; });
    };
    handCardManager.prototype.operateCards = function (hover, choose, inc, fromCnt, toCnt) {
        if (inc === void 0) { inc = true; }
        if (fromCnt === void 0) { fromCnt = 0; }
        if (toCnt === void 0) { toCnt = this.selectCnt.value; }
        var start = this.selectStartIdx + fromCnt;
        var end = this.selectStartIdx + toCnt;
        var from = Math.max(0, Math.min(start, end)) + (inc ? 0 : (fromCnt > 0 ? +1 : -1));
        var to = Math.min(this.cards.length - 1, Math.max(start, end));
        for (var i = from; i <= to; i++) {
            if (i < this.cards.length && i >= 0) {
                if (choose != -1)
                    this.cards[i].choose(choose);
                if (hover != -1)
                    this.cards[i].hover(hover);
            }
        }
    };
    handCardManager.prototype.playOut = function () {
        var _this = this;
        this.chooseIdxs.map(function (idx) {
            _this.anchor.removeChild(_this.cards[idx].node);
            _.pullAt(_this.cards, idx);
        });
        this.cards.map(function (card, idx) {
            card.Idx = idx;
        });
        this.centifyCard();
    };
    handCardManager.prototype.getPlayOut = function () {
        var _this = this;
        var outcardsInfo = [];
        this.chooseIdxs.map(function (idx) {
            outcardsInfo.push(_this.cards[idx].info);
        });
        return outcardsInfo;
    };
    return handCardManager;
}(cardsShower));
exports.handCardManager = handCardManager;

cc._RF.pop();